const Base = require('../../base.js');
const common = require('../../../common/index.js');
const crypto = require('crypto');
var request = require('request')
/*推送api*/
module.exports = class extends Base {
    async indexAction() {
        const cm = new common()
        let cs = {}
        cs.request_id = cm.randomString(32)
        cs.audience ="all"
        // cs.audience.cid = this.get("cid").split(",")
        cs.push_message={}
        cs.push_message.notification={
            title:this.get("title"),
            body:this.get("body"),
            click_type:"url",
            url:this.get("url")
        }
        let res = await this.singleCid(cs)
        this.success(res)
    }

    async singleCid(data) {
        const cacheToken = await this.cache('Gtoken');
        return new Promise((resolve, reject) => {
            if(!cacheToken){
                
            }
            request({
                url: this.config('BaseUrl') + '/push/single/cid',
                method: 'POST',
                headers: {
                    "content-type": "application/json;charset=utf-8",
                    "token": cacheToken
                },
                body: JSON.stringify(data)
            }, function (err, response, body) {
                console.log(body);
                body = JSON.parse(body)
                let data = body
                resolve(data)
            });
        })

    };
}
